import axios from 'axios'
import baseURL from './baseUrl'
const service = axios.create({
  baseURL: 'http://www.mo.top/api', //正式服
  // baseURL: 'http://www.mo2.top/api',//测试服
  timeout: 15000
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    // 添加请求头等前置处理
    config.headers['X-Access-Token'] = localStorage.getItem('token')
    return config
  },
  error => {
    // 请求错误处理
    console.log('Request Error2:', error)
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    // 响应后处理
    if (response.status === 200 && response.data.code === 1) {
      console.log(response,'response')
      return Promise.resolve(response.data)
    } else {
      
      return Promise.resolve(response.data)
    }
  },
  error => {
    console.log('Response Error2:', error)
    // if(error.response.data.message.indexOf('Token失效') != -1){
    //   localStorage.clear();
    //   return  
    // }
    return Promise.reject(error)
  }
)

export default service
